/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GestionAlbumes;

import GestionUsuarios.UsuarioDAO;
import Navegacion.Pagina;
import Navegacion.Paginas;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrador
 */
public class GestorAlbumes extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {

        request.setAttribute("nuevo_link", Paginas.NUEVO.getUrl());
        request.setAttribute("editar_link", Paginas.MODIF.getUrl());
        request.setAttribute("eliminar_link", Paginas.ELIMIN.getUrl());
        request.setAttribute("listar_link", Paginas.LIST.getUrl());
                request.setAttribute("default", Paginas.DEFAULTIMG.getUrl());

        int id = 0;
        if (request.getParameter("id") != null) {
            id = Integer.parseInt(request.getParameter("id"));
        }

        if (request.getParameter("ok") != null) {
            if(request.getParameter("eliminar") != null){
                new AlbumDAO(id).eliminar();
                response.sendRedirect(Paginas.LIST.getUrl());
            }
            String nombre = request.getParameter("nombre");
            String descripcion = request.getParameter("descripcion");
            UsuarioDAO u = (UsuarioDAO) request.getAttribute("usuarioActivo");
            if (request.getParameter("idPrevio").isEmpty()) {
                    new AlbumDAO(nombre, descripcion, u.getId()).guardar();
                } else {
                    new AlbumDAO(Integer.parseInt(request.getParameter("idPrevio")), nombre, descripcion, u.getId()).guardar();
                }
            response.sendRedirect(Paginas.LIST.getUrl());
        }

        if (request.getParameter("modificar") != null) {
            request.setAttribute("edit", true);
            request.setAttribute("form_action", Paginas.MODIF.getUrl() + id);
            AlbumDAO a = new AlbumDAO(id);
            a.cargar();
            request.setAttribute("album",a);
            request.getRequestDispatcher(Paginas.F0RM.getUrl()).forward(request, response);
        } else if (request.getParameter("eliminar") != null) {
            Pagina.aviso("Eliminar album", "Realmente quiere eliminar este album?<br>"+
                    " <a href='"+Paginas.ELIMIN.getUrl()+id+"&ok'>Continuar</a>", request, response);
        }else if (request.getParameter("listar") != null) {
            UsuarioDAO u = (UsuarioDAO) request.getAttribute("usuarioActivo");
            ArrayList<AlbumDAO> albumes = new AlbumesDAO(u.getId()).getAlbumes();
            request.setAttribute("albumes", albumes);
            if (albumes.size() > 0) {
                request.setAttribute("nalb", true);
            } else {
                request.setAttribute("nalb", false);
            }
            request.getRequestDispatcher(Paginas.LISTALBM.getUrl()).forward(request, response);
        }else if (request.getParameter("nuevo") != null) {
            request.setAttribute("edit", false);
            request.setAttribute("form_action", Paginas.NUEVO.getUrl());
            request.getRequestDispatcher(Paginas.F0RM.getUrl()).forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(GestorAlbumes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(GestorAlbumes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
